using Microsoft.EntityFrameworkCore;
using MyHomeworkWebAPi.Models.ef;
using MyHomeworkWebAPi.Models.Request;
using MyHomeworkWebAPi.Models.response;

namespace MyHomeworkWebAPi.Services
{
    /// <summary>
    /// 互动服务实现类
    /// </summary>
    public class InteractionServices : IInteractionServices
    {
        private readonly SecondHandTradingPlatformContext _context;

        /// <summary>
        /// 构造函数，注入数据库上下文
        /// </summary>
        /// <param name="context">数据库上下文</param>
        public InteractionServices(SecondHandTradingPlatformContext context)
        {
            _context = context;
        }

        /// <summary>
        /// 获取所有互动记录
        /// </summary>
        public async Task<List<Interaction>> GetAllAsync()
        {
            return await _context.Interactions.ToListAsync();
        }

        /// <summary>
        /// 根据ID获取互动记录
        /// </summary>
        public async Task<Interaction?> GetByIdAsync(Guid interactionId)
        {
            return await _context.Interactions.FindAsync(interactionId);
        }

        /// <summary>
        /// 创建互动记录
        /// </summary>
        public async Task<Interaction> CreateAsync(InteractionCreateRequest request)
        {
            var interaction = new Interaction
            {
                InteractionId = Guid.NewGuid(),
                UserId = request.UserId,
                ProductId = request.ProductId,
                Type = request.Type,
                CreateTime = DateTime.Now
            };

            await _context.Interactions.AddAsync(interaction);
            await _context.SaveChangesAsync();
            return interaction;
        }

        /// <summary>
        /// 删除互动记录
        /// </summary>
        public async Task<bool> DeleteAsync(Guid interactionId)
        {
            var interaction = await _context.Interactions.FindAsync(interactionId);
            if (interaction == null)
            {
                return false;
            }

            _context.Interactions.Remove(interaction);
            await _context.SaveChangesAsync();
            return true;
        }

        /// <summary>
        /// 获取用户的互动记录列表
        /// </summary>
        public async Task<List<Interaction>> GetByUserIdAsync(Guid userId)
        {
            return await _context.Interactions
                .Where(i => i.UserId == userId)
                .ToListAsync();
        }

        /// <summary>
        /// 获取商品的互动记录列表
        /// </summary>
        public async Task<List<Interaction>> GetByProductIdAsync(Guid productId)
        {
            return await _context.Interactions
                .Where(i => i.ProductId == productId)
                .ToListAsync();
        }

        /// <summary>
        /// 检查用户是否已收藏商品
        /// </summary>
        public async Task<bool> IsProductFavoritedAsync(Guid userId, Guid productId)
        {
            return await _context.Interactions
                .AnyAsync(i => i.UserId == userId && i.ProductId == productId && i.Type == 1);
        }

        /// <summary>
        /// 获取互动统计数据
        /// </summary>
        public async Task<InteractionStatistics> GetStatisticsAsync()
        {
            var today = DateTime.Today;
            var yesterday = today.AddDays(-1);
            var lastWeek = today.AddDays(-7);

            // 获取今日点赞数
            var todayLikes = await _context.Interactions
                .CountAsync(i => i.CreateTime.Date == today && i.Type == 1);

            // 获取昨日点赞数
            var yesterdayLikes = await _context.Interactions
                .CountAsync(i => i.CreateTime.Date == yesterday && i.Type == 1);

            // 获取今日收藏数
            var todayFavorites = await _context.Interactions
                .CountAsync(i => i.CreateTime.Date == today && i.Type == 2);

            // 获取昨日收藏数
            var yesterdayFavorites = await _context.Interactions
                .CountAsync(i => i.CreateTime.Date == yesterday && i.Type == 2);

            // 获取总互动数
            var totalInteractions = await _context.Interactions.CountAsync();

            // 获取上周互动数
            var lastWeekInteractions = await _context.Interactions
                .CountAsync(i => i.CreateTime.Date >= lastWeek && i.CreateTime.Date < today);

            // 计算点赞趋势
            var likesTrend = yesterdayLikes == 0 ? 100 :
                ((double)(todayLikes - yesterdayLikes) / yesterdayLikes) * 100;

            // 计算收藏趋势
            var favoritesTrend = yesterdayFavorites == 0 ? 100 :
                ((double)(todayFavorites - yesterdayFavorites) / yesterdayFavorites) * 100;

            // 计算互动趋势
            var interactionsTrend = lastWeekInteractions == 0 ? 100 :
                ((double)(totalInteractions - lastWeekInteractions) / lastWeekInteractions) * 100;

            return new InteractionStatistics
            {
                TodayLikes = todayLikes,
                LikesTrend = likesTrend,
                TodayFavorites = todayFavorites,
                FavoritesTrend = favoritesTrend,
                TotalInteractions = totalInteractions,
                InteractionsTrend = interactionsTrend
            };
        }
    }
} 